package com.winter.filter;

import org.apache.shiro.authc.*;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 用户登录验证
 * 
// * @see com.mklinfo.snmt.server.b.security.config.ShiroConfig
 * 
 anon     org.apache.shiro.web.filter.authc.AnonymousFilter
 -> authc   org.apache.shiro.web.filter.authc.FormAuthenticationFilter
 authcBasic  org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
 logout  org.apache.shiro.web.filter.authc.LogoutFilter
 noSessionCreation   org.apache.shiro.web.filter.session.NoSessionCreationFilter
 perms   org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
 port    org.apache.shiro.web.filter.authz.PortFilter
 rest    org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
 roles   org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
 ssl     org.apache.shiro.web.filter.authz.SslFilter
 user    org.apache.shiro.web.filter.authc.UserFilter
 * 
 * @author yujun.yao
 *
 */
public class FormAuthenticationFilter extends org.apache.shiro.web.filter.authc.FormAuthenticationFilter {
    public static final String DEFAULT_CAPTCHA_PARAM = "validateCode";
    public static final String DEFAULT_MOBILE_PARAM = "mobileLogin";
    public static final String DEFAULT_MESSAGE_PARAM = "message";
    private String captchaParam = "validateCode";
    private String mobileLoginParam = "mobileLogin";
    private String messageParam = "message";

    protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
        String username = getUsername(request);
        String password = getPassword(request);
        if (password == null) {
            password = "";
        }
        boolean rememberMe = isRememberMe(request);
        String host = request.getRemoteAddr();
        String captcha = getCaptcha(request);
        boolean mobile = isMobileLogin(request);
//        return new UsernamePasswordToken(username, password.toCharArray(), rememberMe, host, captcha, mobile,false);
        return new UsernamePasswordToken(username, password.toCharArray());
    }

    public String getCaptchaParam() {
        return this.captchaParam;
    }

    protected String getCaptcha(ServletRequest request) {
        return WebUtils.getCleanParam(request, getCaptchaParam());
    }

    public String getMobileLoginParam() {
        return this.mobileLoginParam;
    }

    protected boolean isMobileLogin(ServletRequest request) {
        return WebUtils.isTrue(request, getMobileLoginParam());
    }

    public String getMessageParam() {
        return this.messageParam;
    }

    public String getSuccessUrl() {
        return super.getSuccessUrl();
    }

    protected void issueSuccessRedirect(ServletRequest request, ServletResponse response) throws Exception {
        WebUtils.issueRedirect(request, response, getSuccessUrl(), null, true);
    }

    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request,
                                     ServletResponse response) {
        String className = e.getClass().getName();
        String message = "";
        if ((IncorrectCredentialsException.class.getName().equals(className))
                || (UnknownAccountException.class.getName().equals(className))) {
            message = "用户名或密码错误，请重试";
        } else if ((e.getMessage() != null)) {
            message = e.getMessage();
        } else {
            message = "系统异常，请稍后再试";
            e.printStackTrace();
        }
        request.setAttribute(getFailureKeyAttribute(), className);
        request.setAttribute(getMessageParam(), message);
        return true;
    }
}
